/**
 * Created by Administrator on 2017/6/1 0001.
 */
define(["jquery", "template", "scprit/department/view/template/follow-up/follow-up-tpl.js"], function($, template, tpl) {
    var question = {};
    var inTable = function(data) {
        return template("babyInfo-tpl", data);
    };
    var inTable2 = function(data) {
        return template("babyList-tpl", data);
    };
    var inTableNew = function(data) {
        return template("babyInfo-new-tpl", data);
    };
    var inTableHistory = function(data) {
        return template("history-tpl", data);
    };
    var inTableSearch = function(data) {
        return template("searchBox-tpl", data);
    };
    var inTableSearchList = function(data) {
        return template("searchList-tpl", data);
    };
    question.infoInit = function(options) {
        $("body").append(tpl.init());
        var html = inTable(options.data);
        $(options.id).html(html);
    };
    question.infoInitNew = function(options) {
        $("body").append(tpl.init3());
        var html = inTableNew(options.data);
        $(options.id).html(html);
    };
    question.listInit = function(options) {
        $("body").append(tpl.init2());
        var html = inTable2(options);
        $(options.id).html(html);
    };
    question.historyInit = function(options) {
        $("body").append(tpl.init4());
        var html = inTableHistory(options);
        $(options.id).html(html);
    };
    question.searchBoxInit = function(options) {
        $("body").append(tpl.init5());
        var html = inTableSearch(options);
        $(options.id).append(html);
    };
    question.searchListInit = function(options) {
        $("body").append(tpl.init6());
        var html = inTableSearchList(options);
        $(options.id).html(html);
    };
    //患者信息数据处理：
    question.babyInfoDeal = function(data) {
        var resData = {};
        resData.userName = data.userName == '' ? "未知" : data.userName;
        resData.pretermBirth = 37 * 7 > data.pregnancyDay ? "是" : "否";
        var nowDate = this.getNowFormatDate();
        var acturalDay = this.DateDiff(nowDate, data.birthday);
        var age = this.age(acturalDay, data.pregnancyDay);
        var monthAge = this.monthAge(acturalDay, data.pregnancyDay);
        if (acturalDay < 365 * 2 && resData.pretermBirth == "是") {
            resData.itemName = "纠正年龄";
        } else {
            resData.itemName = "实际年龄";
        }
        resData.monthAge = monthAge.toFixed(1);
        resData.weight = data.weight == undefined ? "未知" : data.weight + "(g)";
        resData.age = age;
        resData.hospitalNumber = data.delivery == undefined ? "未知" : data.delivery; //分娩住院号
        resData.clinic = data.outpatient == undefined ? "未知" : data.outpatient; //门诊号
        resData.card = data.socialCard == undefined ? "未知" : data.socialCard; //社保卡号；
        resData.selfManagementCard = data.cardNo == undefined ? "未知" : data.cardNo; //自管卡号
        return resData;
    };
    question.ListDeal = function(data) {
        for (var i = 0; i < data.length; i++) {
            data[i].pretermBirth = 37 * 7 > data[i].pregnancyDay ? "是" : "否";
            var nowDate = this.getNowFormatDate();
            var monthAge = this.DateDiff(nowDate, data[i].birthday) / 30;
            data[i].monthAge = monthAge.toFixed(1);
            data[i].birthWeight = data[i].birthWeight == undefined ? "未知" : data[i].birthWeight + "(g)";
            data[i].inpatientNo = data[i].inpatientNo == undefined ? "未知" : data[i].inpatientNo;
            data[i].outPatientNo = data[i].outPatientNo == undefined ? "未知" : data[i].outPatientNo;
            data[i].socialCardNo = data[i].socialCardNo == undefined ? "未知" : data[i].socialCardNo;
            data[i].selfCardNo = data[i].selfCardNo == undefined ? "未知" : data[i].selfCardNo;
        }
        return data;
    };
    question.yearDayWeek = function(acturalDay, pregnance) {
        var age = {},
            day = acturalDay;
        pregnance = pregnance == 0 ? 280 : pregnance;
        if (acturalDay < 365 * 2 && pregnance < 37 * 7) {
            day = acturalDay - (280 - pregnance);
        }
        if (day < 0) {
            age.year = 0;
            age.week = 0;
            age.day = day;
        } else {
            age.year = Math.floor(day / 365);
            age.week = Math.floor(day % 365 / 7);
            age.day = Math.floor(day % 365 % 7);
        }
        return age;
    };
    //返回当前时间：
    question.getNowFormatDate = function() {
            var date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }
        //计算天数差的函数，通用 ：
    question.DateDiff = function(sDate1, sDate2) {
        function addZore(num) { return num < 10 ? '0' + num : num }
        /**
         * 兼容 时间戳 格式的日期
         */
        sDate2 = String(sDate2).indexOf('-') > -1 ? String(sDate2) : Number(sDate2)
        var d1 = new Date(sDate1)
        var d2 = new Date(sDate2)
        sDate1 = d1.getFullYear() + '-' + addZore(d1.getMonth() + 1) + '-' + addZore(d1.getDate())
        sDate2 = d2.getFullYear() + '-' + addZore(d2.getMonth() + 1) + '-' + addZore(d2.getDate())

        var aDate, oDate1, oDate2, iDays;
        aDate = sDate1.split("-")
        oDate1 = new Date(parseFloat(aDate[0]), parseFloat(aDate[1]) - 1, parseFloat(aDate[2])); //创建时间；
        aDate = sDate2.split("-")
        oDate2 = new Date(parseFloat(aDate[0]), parseFloat(aDate[1]) - 1, parseFloat(aDate[2])); //创建时间；
        iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
        return iDays
    }
        //从数组中移除某个值
    question.removeByValue = function(arr, val) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    }
    question.contains = function(arr, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] === obj) {
                return true;
            }
        }
        return false;
    }
    question.date = function() {
        // 对Date的扩展，将 Date 转化为指定格式的String
        // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
        // 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
        // 例子：
        // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2017-02-02 08:09:04.423
        // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2017-2-2 8:9:4.18
        Date.prototype.Format = function(fmt) { //author: meizz
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
    };
    //实际年龄：
    question.getAge = function(day) {
            var y = parseInt(day / 365) == 0 ? "" : parseInt(day / 365);
            var m = parseInt(day % 365 / 30.4) == 0 ? "" : parseInt(day % 365 / 30.4);
            var w = parseInt(day % 365 % 30.4 / 7) == 0 ? '' : parseInt(day % 365 % 30.4 / 7);
            var d = parseInt(day % 365 % 30.4 % 7) == 0 ? '' : parseInt(day % 365 % 30.4 % 7);
            var age;
            if (y) {
                age = y + "岁" + (m >= 1 ? m + "月龄" : "");
            } else {
                if (m >= 3) {
                    age = m + "月龄" + (parseInt(day % 365 % 30.4)) + "天";
                } else {
                    var days = day;
                    age = (parseInt(days / 7) > 0 ? parseInt(days / 7) + "周" : "") + parseInt(days % 7) + "天";
                }
            }
            return age
        }
        //纠正年龄：
    question.age = function(acturalDay, pregnance) {
            var day, age2;
            if (pregnance == 0) { pregnance = 280; }
            if (parseFloat(pregnance) < 37 * 7 && acturalDay < 2 * 365) {
                var difD = 40 * 7 - parseFloat(pregnance); //判断与预产期差了多少天；
                day = acturalDay - difD;
                if (day > 0) {
                    age2 = this.getAge(day);
                } else {
                    age2 = day + "天";
                }
            } else {
                day = acturalDay;
                age2 = this.getAge(day);
            }
            return age2;
        }
        //纠正月龄：
    question.monthAge = function(acturalDay, pregnance) {
        var day, monthAge;
        if (pregnance == 0) { pregnance = 280; }
        if (parseFloat(pregnance) < 37 * 7 && acturalDay < 2 * 365) {
            var difD = 40 * 7 - parseFloat(pregnance); //判断与预产期差了多少天；
            day = acturalDay - difD;
            if (day > 0) {
                monthAge = (day / 30.4);
            } else {
                monthAge = 0;
            }
        } else {
            day = acturalDay;
            monthAge = (day / 30.4);
        }
        return monthAge;
    }
    question.hashmap = function() {
        function HashMap() {
            this.map = {};
        }
        HashMap.prototype = {
            put: function(key, value) {
                this.map[key] = value;
            },
            get: function(key) {
                if (this.map.hasOwnProperty(key)) {
                    return this.map[key];
                }
                return null;
            },
            remove: function(key) {
                if (this.map.hasOwnProperty(key)) {
                    return delete this.map[key];
                }
                return false;
            },
            removeAll: function() {
                this.map = {};
            },
            keySet: function() {
                var _keys = [];
                for (var i in this.map) {
                    _keys.push(i);
                }
                return _keys;
            }
        };
        HashMap.prototype.constructor = HashMap;
        return HashMap;
    }
    return question;
});